package com.ruoyi.web.controller.app;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Maps;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.AppUserCollect;
import com.ruoyi.system.service.IAppUserCollectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/app/userCollect")
@Api("APP用户收藏接口")
public class ForAppUserCollectController extends BaseController {

    @Autowired
    private IAppUserCollectService userCollectService;

    @PostMapping("getCollectPage")
    @ApiOperation("获取用户收藏列表")
    public AjaxResult getCollectPage(@ApiParam("userId") Long userId,
                                     @ApiParam("页码") @RequestParam(defaultValue = "1") int page,
                                     @ApiParam("条数") @RequestParam(defaultValue = "20") int limit,
                                     @ApiParam("类型，不传为全部，0：笔记（纯文字），1：图片url，2：视频url，3：链接url") Integer type) {
        QueryWrapper<AppUserCollect> queryWrapper = Wrappers.query();
        queryWrapper.eq("user_id", getUserId());
        if (type != null) {
            queryWrapper.eq("type", type);
        }
        queryWrapper.orderByDesc("create_time");

        Page obj = userCollectService.page(new Page<>(page, limit), queryWrapper);

        Map<String, Object> ret = Maps.newHashMap();
        ret.put("totalPage", obj.getPages());
        ret.put("totalCount", obj.getTotal());
        ret.put("list", obj.getRecords());
        return AjaxResult.ok(0, "", ret);
    }

    @PostMapping("getCollectInfo")
    @ApiOperation("获取用户收藏信息")
    public AjaxResult getCollectInfo(@ApiParam("收藏id") @RequestParam Long collectId) {
        Map<String, Object> map = new HashMap<>();
        map.put("collectInfo", userCollectService.getById(collectId));
        return AjaxResult.ok(0, "", map);
    }

    @PostMapping("addUserCollect")
    @ApiOperation("添加用户收藏")
    public AjaxResult addUserCollect(@ApiParam("userId") Long userId,
                                     @ApiParam("类型，0：笔记（纯文字），1：图片url，2：视频url，3：链接url") @RequestParam Integer type,
                                     @ApiParam("笔记内容、图片视频链接url、富文本") @RequestParam String content,
                                     @ApiParam("视频封面") String videoCover) {
        if (type == 2 && StringUtils.isBlank(videoCover)) {
            return AjaxResult.error(-1, "视频封面不能为空");
        }
        AppUserCollect userCollectEntity = new AppUserCollect();
        userCollectEntity.setCreateTime(new Date());
        userCollectEntity.setUserId(getUserId());
        userCollectEntity.setType(type);
        userCollectEntity.setContent(content);
        userCollectEntity.setVideoCover(videoCover);
        userCollectService.save(userCollectEntity);
        Map<String, Object> map = new HashMap<>();
        map.put("collectInfo", userCollectEntity);
        return AjaxResult.ok(0, "", map);
    }

    @PostMapping("delUserCollect")
    @ApiOperation("删除用户收藏")
    public AjaxResult delUserCollect(@ApiParam("收藏id") @RequestParam Long collectId) {
        userCollectService.removeById(collectId);
        return AjaxResult.ok(0, "删除成功", null);
    }

}
